package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartRestController {

    @Autowired
    CartService cartService;

    /**
     * 获取购物车中所有商品列表
     *
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList() {

        String cartKey = cartService.determinCartKey();
        List<CartInfo> cartInfos = cartService.getCartList(cartKey);
        return Result.ok(cartInfos);
    }

    /**
     * 修改购物车中的商品数量
     *
     * @param skuId
     * @param num
     * @return
     */
    @PostMapping("/addToCart/{skuId}/{num}")
    public Result updateItemCount(@PathVariable("skuId") Long skuId,
                                  @PathVariable("num") Integer num) {

        //根据键确定修改那个购物车
        String cartKey = cartService.determinCartKey();

        cartService.updateItemCount(cartKey,skuId,num);
        return Result.ok();
    }

    @GetMapping("checkCart/{skuId}/{status}")
    public  Result checkItem(@PathVariable("skuId") Long skuId,
                             @PathVariable("status") Integer status){

        //根据键确定修改那个购物车
        String cartKey = cartService.determinCartKey();
        cartService.updateItemCheckStatus(cartKey,skuId,status);

        return Result.ok();
    }

    /**
     * 删除指定的商品
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteItem(@PathVariable("skuId") Long skuId){
        String cartKey = cartService.determinCartKey();
        cartService.deleteCartItem(cartKey,skuId);
        return Result.ok();
    }
}
